package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.adapter.PictureAdapter;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.PraiseBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司17
 * Created by 20433 on 2018/1/29
 */

public class PictureActivity extends Activity {
    @Bind(R.id.activity_picture_viewpager)
    ViewPager activityPictureViewpager;
    @Bind(R.id.activity_picture_iv_return)
    ImageView activityPictureIvReturn;
    @Bind(R.id.activity_picture_rl_return)
    RelativeLayout activityPictureRlReturn;
    @Bind(R.id.activity_picture_tv_content)
    TextView activityPictureTvContent;
    @Bind(R.id.activity_picture_iv_thumb_up)
    ImageView activityPictureIvThumbUp;
    @Bind(R.id.activity_picture_tv_thumb_up)
    TextView activityPictureTvThumbUp;
    @Bind(R.id.activity_picture_rl_thumb_up)
    RelativeLayout activityPictureRlThumbUp;
    @Bind(R.id.activity_picture_iv_commentaries)
    ImageView activityPictureIvCommentaries;
    @Bind(R.id.activity_picture_tv_commentaries)
    TextView activityPictureTvCommentaries;
    @Bind(R.id.activity_picture_rl_to_ckeck_dynamic)
    RelativeLayout activityPictureRlToCkeckDynamic;
    private List<String> images = new ArrayList();
    private List<View> picture = new ArrayList();

    private PictureAdapter pictureAdapter;
    private String po_id;
    private String po_name;
    private String addtime;
    private String video;
    private String vpic;
    private String content;
    private String cat_id;
    private String cus_id;
    private String cus_name;
    private String cus_logo;
    private String count;
    private int hits_like;
    private int lstate;
    private int col;
    private int state;
    private int lCount;
    private int po_url;
    private List<String> dataBean;
    private int postion;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, false, false);
        Utils.setStatusTextColor(false, this);

        Intent intent = getIntent();
        po_id = intent.getStringExtra("po_id");
        po_name = intent.getStringExtra("po_name");
        addtime = intent.getStringExtra("addtime");
        video = intent.getStringExtra("video");
        vpic = intent.getStringExtra("vpic");
        content = intent.getStringExtra("content");
        cat_id = intent.getStringExtra("cat_id");
        cus_id = intent.getStringExtra("cus_id");
        cus_name = intent.getStringExtra("cus_name");
        cus_logo = intent.getStringExtra("cus_logo");
        count = intent.getStringExtra("count");
        hits_like = intent.getIntExtra("hits_like",0);
        lstate = intent.getIntExtra("lstate",0);
        col = intent.getIntExtra("col",0);
        state = intent.getIntExtra("state",0);
        lCount = intent.getIntExtra("lCount",0);
        po_url = intent.getIntExtra("po_url",0);

        dataBean = (List<String>) intent.getSerializableExtra("po_url");
        if(dataBean == null){
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
        postion = intent.getIntExtra("postion",0);

        activityPictureTvThumbUp.setText(hits_like+"");
        if(lstate == 0){
            activityPictureIvThumbUp.setImageResource(R.mipmap.zan_05);
        }else{
            activityPictureIvThumbUp.setImageResource(R.mipmap.zan_02);
        }

        if(dataBean != null && dataBean.size() > 0){
            for (String s : dataBean) {
                images.add("http://oop.welbeing.cn/Public/Uploads/" +s);
                picture.add(View.inflate(this,R.layout.picture_01,null));
            }

        }
        if(hits_like >= 0){
            activityPictureTvThumbUp.setText(hits_like +"");
        }
        if( count != null && TextUtils.isEmpty(count)){
            activityPictureTvCommentaries.setText(count);
        }
        if(lstate == 0){
            activityPictureIvThumbUp.setBackgroundResource(R.mipmap.zan_08);
        }else if(lstate == 1){
            activityPictureIvThumbUp.setBackgroundResource(R.mipmap.zan_07);
        }
        activityPictureViewpager.setCurrentItem(postion);
        //activityPictureTvContent.setText(introarr[po_select_pictrue]);
        pictureAdapter = new PictureAdapter(images, picture, PictureActivity.this);
        activityPictureViewpager.setAdapter(pictureAdapter);

    }
    @OnClick({R.id.activity_picture_iv_return, R.id.activity_picture_iv_thumb_up,R.id.activity_picture_rl_to_ckeck_dynamic})
    public void onViewClicked(View paramView){
        switch (paramView.getId()){
            case R.id.activity_picture_iv_return:
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                break;
            case R.id.activity_picture_iv_thumb_up:
                giveLike();
                break;
            case R.id.activity_picture_rl_to_ckeck_dynamic:
                Intent  intent = new Intent(this, ReleaseSimilarProfilePageFocusedActivity.class);
                intent.putExtra("postion",postion);
                intent.putExtra("po_id",po_id);
                intent.putExtra("po_name",po_name);
                intent.putExtra("addtime",addtime);
                intent.putExtra("video",video);
                intent.putExtra("vpic",vpic);
                intent.putExtra("content",content);
                intent.putExtra("cat_id",cat_id);
                intent.putExtra("cus_id",cus_id);
                intent.putExtra("cus_name",cus_name);
                intent.putExtra("cus_logo",cus_logo);
                intent.putExtra("hits_like",hits_like);
                intent.putExtra("lstate",lstate);
                intent.putExtra("col",col);
                intent.putExtra("state",state);
                intent.putExtra("count",count);
                intent.putExtra("lCount",lCount);
                intent.putExtra("po_url", (Serializable)dataBean);
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                break;

        }
    }

    /**
     * @desc 点赞
     * */
    private void giveLike() {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String  user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.PRAISE)
                .addParams("cus_id", user_id)
                .addParams("cat_id",cat_id)
                .addParams("po_id", po_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                       // Log.e(TAG,"praiseParseNetworkResponse======555======>"+praiseParseNetworkResponse);
                        final PraiseBean praiseBean =  gson.fromJson(praiseParseNetworkResponse,PraiseBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(praiseBean.errorCode == 200){
                                    if(praiseBean.errorMessage.equals("点赞成功!")){
                                        hits_like = hits_like +1;
                                        activityPictureTvThumbUp.setText(hits_like+"");
                                        activityPictureIvThumbUp.setImageResource(R.mipmap.zan_10);
                                    }else if(praiseBean.errorMessage.equals("点赞取消!")){
                                        hits_like = hits_like - 1;
                                        activityPictureTvThumbUp.setText(hits_like+"");
                                        activityPictureIvThumbUp.setImageResource(R.mipmap.zan_05);
                                    }
                                }else{
                                    Toast.makeText(PictureActivity.this,praiseBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(PictureActivity.this,"网络异常",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
}
